package com.tf.mxj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tf.mxj.entity.Consult;
import com.tf.mxj.service.ConsultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/consult")
public class ConsultController {

    @Autowired
    private ConsultService consultService;

    // 查询所有咨询
    @GetMapping("/list")
    public Map<String, Object> list() {
        List<Consult> list = consultService.list(new QueryWrapper<Consult>().eq("is_deleted", 0));
        Map<String, Object> map = new HashMap<>();
        map.put("code", 20000);
        map.put("data", list);
        return map;
    }

    // 新增咨询
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody Consult consult) {
        consult.setId(UUID.randomUUID().toString().replace("-", ""));
        consult.setIsDeleted(0);
        consult.setStatus(0); // 默认待回复
        consult.setGmtCreate(new Date());
        consult.setGmtModified(new Date());
        boolean result = consultService.save(consult);
        Map<String, Object> map = new HashMap<>();
        map.put("code", result ? 20000 : 50000);
        map.put("success", result);
        map.put("message", result ? "操作成功，咨询已提交。" : "操作失败，咨询提交未完成。");
        return map;
    }

    // 修改咨询
    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody Consult consult) {
        consult.setGmtModified(new Date());
        boolean result = consultService.updateById(consult);
        Map<String, Object> map = new HashMap<>();
        map.put("code", result ? 20000 : 50000);
        map.put("success", result);
        map.put("message", result ? "操作成功，咨询信息已更新。" : "操作失败，咨询信息更新未完成。");
        return map;
    }

    // 逻辑删除
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable String id) {
        Consult consult = new Consult();
        consult.setId(id);
        consult.setIsDeleted(1);
        consult.setGmtModified(new Date());
        boolean result = consultService.updateById(consult);
        Map<String, Object> map = new HashMap<>();
        map.put("code", result ? 20000 : 50000);
        map.put("success", result);
        map.put("message", result ? "操作成功，咨询已删除。" : "操作失败，咨询删除未完成。");
        return map;
    }

}

